home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
ARGONET
/
PD
/
DTP
/
PDFENCRY.SPK
/
rc4_cc
< prev
next >
Wrap
Text File
|
1998-04-04
|
1KB
|
71 lines
/************************************************************************
* *
* RC4 Encryption Algorithm *
* Copyright Peter Gutmann 1995-1996 *
* *
************************************************************************/
/* Optimized RC4 code, from an unknown source ("and they knew not from whence
it had come...") */
#include "rc4.h"
void
rc4ExpandKey( RC4KEY *rc4, unsigned char const *key, int keylen )
{
int x, keypos = 0;
rc4word sx, y = 0;
rc4word *state = &rc4->state[ 0 ];
rc4->x = rc4->y = 0;
for( x = 0; x < 256; x++ )
state[ x ] = x;
for( x = 0; x < 256; x++ )
{
sx = state[ x ];
y += sx + key[ keypos ];
#ifdef USE_LONG_RC4
y &= 0xFF;
#endif /* USE_LONG_RC4 */
state[ x ] = state[ y ];
state[ y ] = sx;
if( ++keypos == keylen )
keypos = 0;
}
}
void
rc4Crypt( RC4KEY *rc4, unsigned char *data, int len )
{
rc4word x = rc4->x, y = rc4->y;
rc4word sx, sy;
rc4word *state = &rc4->state[ 0 ];
while (len--) {
x++;
#ifdef USE_LONG_RC4
x &= 0xFF;
#endif /* USE_LONG_RC4 */
sx = state[ x ];
y += sx;
#ifdef USE_LONG_RC4
y &= 0xFF;
#endif /* USE_LONG_RC4 */
sy = state[ y ];
state[ y ] = sx;
state[ x ] = sy;
#ifdef USE_LONG_RC4
*data++ ^= state[ ( unsigned char ) ( sx+sy ) ];
#else
*data++ ^= state[ ( sx+sy ) & 0xFF ];
#endif /* USE_LONG_RC4 */
}
rc4->x = x;
rc4->y = y;
}